package com.rc.saas.mini.service.member;


import com.rc.saas.mini.mapper.member.MemberRefundOrderMapper;
import com.rc.saas.mini.mapper.recharge.RechargeOrderMapper;
import com.rc.saas.mini.model.member.MemberRefundOrder;
import com.rc.saas.mini.model.member.MemberRefundOrderExample;
import com.rc.saas.mini.model.recharge.RechargeOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class MemberRefundOrderServiceImpl implements MemberRefundOrderService {
    @Autowired
    private MemberRefundOrderMapper memberRefundOrderMapper;
    @Autowired
    private RechargeOrderMapper rechargeOrderMapper;

    @Override
    public MemberRefundOrderMapper getMapper() {
        return memberRefundOrderMapper;
    }

    @Override
    public int insertByRechargeOrder(Long rechargeOrderId, String recordType, BigDecimal originalAccount) {
        RechargeOrder rechargeOrder = rechargeOrderMapper.selectByPrimaryKey(rechargeOrderId);
        if (rechargeOrder == null) {
            return 0;
        }

        MemberRefundOrder memberRefundOrder = new MemberRefundOrder();

        memberRefundOrder.setTenantCode(rechargeOrder.getTenantCode());
        memberRefundOrder.setMemberId(rechargeOrder.getMemberId());

        //memberRefundOrder.setOrderNum(rechargeOrder.getOutTradeNo());    //本地自有订单号
        //todo
        //memberRefundOrder.setRechargeOrderId(rechargeOrderId);

        //先付后退充值，不给用户手动申请退款
        if ("17".equals(recordType)) {
            memberRefundOrder.setAccountType("3");//3先付后退钱包
            memberRefundOrder.setRefundType("1");   //退款订单类型 (0.可退 1不可退)
        } else {
            memberRefundOrder.setAccountType("0");//0个人钱包
            memberRefundOrder.setRefundType("0");   //退款订单类型 (0.可退 1不可退)
        }

        memberRefundOrder.setRecordType(recordType);
        memberRefundOrder.setOutOrderNum(rechargeOrder.getTransactionId()); //商户订单号

        memberRefundOrder.setRechargeAmount(rechargeOrder.getAmount());
        memberRefundOrder.setFactAmount(rechargeOrder.getFactAmount());
        memberRefundOrder.setGiveAmount(rechargeOrder.getGiveAmount());

        //可退余额，如果上次充电超充导致账户变成负数，本次充值到账也要加上这些负数
        if (originalAccount != null && BigDecimal.ZERO.compareTo(originalAccount) > 0) {
            memberRefundOrder.setRefundFactAmount(rechargeOrder.getFactAmount().add(originalAccount));
        } else {
            memberRefundOrder.setRefundFactAmount(rechargeOrder.getFactAmount());
        }

        memberRefundOrder.setRefundGiveAmount(rechargeOrder.getGiveAmount());

        memberRefundOrder.setRefundedFactAmount(BigDecimal.ZERO);
        memberRefundOrder.setRefundedGiveAmount(BigDecimal.ZERO);
        memberRefundOrder.setRefundStatus("0");

        memberRefundOrder.setPaymentType(rechargeOrder.getPaymentType());
        memberRefundOrder.setCreatedTime(new Date());

        memberRefundOrderMapper.insertSelective(memberRefundOrder);
        return 1;
    }

    @Override
    public MemberRefundOrder selectLast(Long memberId) {
        MemberRefundOrderExample memberRefundOrderExample = new MemberRefundOrderExample();
        memberRefundOrderExample.createCriteria().andAccountTypeEqualTo("3").andRefundStatusEqualTo("0")
                .andMemberIdEqualTo(memberId);
        memberRefundOrderExample.setOrderByClause(" refund_order_id desc ");

        return memberRefundOrderMapper.selectByExample(memberRefundOrderExample).stream().findFirst().orElseGet(() -> null);
    }
}
